package view;

import Bean.Account;
import java.awt.GraphicsEnvironment;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import model.ConnectDB;

public class LoginDialog extends javax.swing.JDialog
{

    private Connection connect = null;
    private Statement state = null;
    private ResultSet res = null;
    private String priority = "";

    public LoginDialog(java.awt.Frame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        this.setTitle("Đăng nhập");
        this.setResizable(false);
        this.setAlwaysOnTop(true);
        ImageIcon ic = new ImageIcon(LoginDialog.class.getResource("/data/icon.png"));
        this.setIconImage(ic.getImage());
        this.setLocationRelativeTo(null);//xuất hiện giữa màn hình
        this.setVisible(true);
    }

    private boolean checkLogin(String user, String pass)
    {
        try
        {
            ConnectDB.connect();
            connect = ConnectDB.getConnect();
            state = connect.createStatement();
            res = state.executeQuery("SELECT * FROM db_project_hotel_manager_team_ttq.account;");
            while (res.next())
            {
                String userDb = res.getString("User");
                String passDb = res.getString("Pass");
                priority = res.getString("priority");
                if (user.equals(userDb) && pass.equals(passDb))
                {
                    return true;
                }
            }
            res.close();
            state.close();
            ConnectDB.close();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(LoginDialog.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

        return false;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        btn_login = new javax.swing.JButton();
        btn_exit = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        tf_username = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tf_password = new javax.swing.JPasswordField();
        lb_test = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btn_login.setText("Đăng nhập");
        btn_login.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btn_loginActionPerformed(evt);
            }
        });

        btn_exit.setText("Thoát");
        btn_exit.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btn_exitActionPerformed(evt);
            }
        });

        jLabel1.setText("Tên đăng nhập");

        tf_username.setText(" ");
        tf_username.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                tf_usernameActionPerformed(evt);
            }
        });

        jLabel2.setText("Mật khẩu");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/login.png"))); // NOI18N

        lb_test.setForeground(new java.awt.Color(255, 0, 0));
        lb_test.setText(" ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel3)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2))
                                .addGap(41, 41, 41)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tf_username, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE)
                                    .addComponent(tf_password)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btn_login, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                                .addComponent(btn_exit, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(31, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lb_test, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(41, 41, 41)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(tf_password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addComponent(lb_test)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_exit, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_login, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel3))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_tf_usernameActionPerformed
    {//GEN-HEADEREND:event_tf_usernameActionPerformed
        System.exit(0);
    }//GEN-LAST:event_tf_usernameActionPerformed

    private void btn_exitActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btn_exitActionPerformed
    {//GEN-HEADEREND:event_btn_exitActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btn_exitActionPerformed

    private void btn_loginActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btn_loginActionPerformed
    {//GEN-HEADEREND:event_btn_loginActionPerformed
        String user = tf_username.getText().trim();
        String pass = tf_password.getText().trim();

        //Đăng nhập thành công, vào chương trình chính
        if (checkLogin(user, pass))
        {
            this.setVisible(false);
            this.dispose();
            MainFrame mf = new MainFrame(user);
            if (priority.equals("Admin"))
            {
                mf.Show(1);
            }
            else
            {
                mf.Show(0);
            }
            priority = "";
        }
        //Đăng nhập không thành công, thông báo
        else
        {
            lb_test.setText("Bạn đã nhập sai thông tin đăng nhập");
        }
    }//GEN-LAST:event_btn_loginActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_exit;
    private javax.swing.JButton btn_login;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lb_test;
    private javax.swing.JPasswordField tf_password;
    private javax.swing.JTextField tf_username;
    // End of variables declaration//GEN-END:variables
}
